<template>
  <div class="seller-container">
    <div class="sellerchart" ref="seller_ref"></div>
  </div>
</template>

<script>
export default {
  name: "Rank",
  data() {
    return {
      chartInstance: null,
      allData: "",
      timerId: "",
      startValue: 0,
      endValue: 9,
    };
  },
  mounted() {
    this.initChart();
    this.getData();
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    clearInterval(this.timerId);
    window.removeEventListener("resize", this.screenAdapter);
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echart.init(this.$refs.seller_ref, "customed");

      const initOption = {
        title: {
          text: "| 地区销售排行",
          left: 20,
          top: 20,
        },
        grid: {
          top: "25%",
          left: "4%",
          right: "5%",
          bottom: "5%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
        },
        yAxis: {
          type: "value",
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "line",
            lineStyle: {
              width: 60,
              color: "#2d3443",
              type: "solid",
            },
            z: 0,
          },
        },
        series: [
          {
            type: "bar",
            barWidth: 60,
            label: {
              show: true,
              position: "top",
              textStyle: {
                color: "#fff",
              },
            },
            itemStyle: {
              borderRadius: [0, 30, 30, 0],
            },
          },
        ],
      };
      initOption && this.chartInstance.setOption(initOption);

      this.chartInstance.on("mouseover", () => {
        clearInterval(this.timerId);
      });
      this.chartInstance.on("mouseout", () => {
        this.startInterval();
      });
    },
    async getData() {
      const { data: ret } = await this.$http.get("rank");
      this.allData = ret;
      this.allData.sort((a, b) => {
        return b.value - a.value;
      });
      this.updateChart();
      this.startInterval();
    },
    updateChart() {
      const provinceArr = this.allData.map((item) => {
        return item.name;
      });
      const valueArr = this.allData.map((item) => {
        return item.value;
      });
      const colorArr = [
        ["#0ba82C", "#4FF778"],
        ["#2E72BF", "#23E5E5"],
        ["#5052EE", "#AB6EE5"],
      ];
      const dataOption = {
        xAxis: {
          data: provinceArr,
        },
        dataZoom: {
          show: false,
          startValue: this.startValue,
          endValue: this.endValue,
        },
        series: [
          {
            data: valueArr,
            itemStyle: {
              color: (arg) => {
                let targetColorArr = null;
                if (arg.value > 300) {
                  targetColorArr = colorArr[0];
                } else if (arg.value > 200) {
                  targetColorArr = colorArr[1];
                } else {
                  targetColorArr = colorArr[2];
                }
                return new this.$echart.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: targetColorArr[0],
                  },
                  {
                    offset: 1,
                    color: targetColorArr[1],
                  },
                ]);
              },
            },
          },
        ],
      };
      dataOption && this.chartInstance.setOption(dataOption);
    },
    // startInterval() {
    //   this.timerId = setInterval(() => {
    //     this.currentPage++;
    //     if (this.currentPage > this.totalPage) {
    //       this.currentPage = 1;
    //     }
    //     this.updateChart();
    //   }, 3000);
    // },
    startInterval() {
      this.timerId = setInterval(() => {
        this.startValue++;
        this.endValue++;
        if(this.endValue>this.allData.length-1){
          this.startValue=0
          this.endValue=9
        }
        this.updateChart();
      }, 2000);
    },
    screenAdapter() {
      const title = (this.$refs.seller_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: title,
          },
        },
        tooltip: {
          axisPointer: {
            lineStyle: {
              width: title,
            },
          },
        },
        series: [
          {
            barWidth: title,

            itemStyle: {
              borderRadius: [0, 0, 0, 0],
            },
          },
        ],
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
  },
};
</script>

<style>
</style>